package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"path"
	"takeout-ordering/api/client"
	"takeout-ordering/api/model"
	takeout "takeout-ordering/common/proto"
	"takeout-ordering/pkg"
)

func UserRegister(c *gin.Context) {
	var data model.Register
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	user, err := client.UserRegister(c, &takeout.UserRegisterReq{
		Mobile:   data.Mobile,
		Password: data.Password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "用户注册成功",
		"data": user.Success,
	})
}

func UserLogin(c *gin.Context) {
	var data model.Login
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil})
		return
	}
	user, err := client.UserLogin(c, &takeout.UserLoginReq{
		Mobile:   data.Mobile,
		Password: data.Password})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil})
		return
	}
	claims := pkg.CustomClaims{ID: uint(user.UserId)}

	token, err := pkg.NewJWT("2209a").CreateToken(claims)
	if err != nil {
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 0,
				"msg":  "Token生成错误",
				"data": nil})
			return
		}
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "用户登录成功",
		"data": token,
	})
}

func MealsList(c *gin.Context) {
	var data model.MealsList
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	user, err := client.MealsList(c, &takeout.MealsListReq{
		Types: data.Types,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "餐品列表展示成功",
		"data": user,
	})
}

func Upload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil})
		return
	}
	if file.Size > 5*1024*1024 {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "文件过大",
			"data": nil})
		return
	}
	File := map[string]bool{
		".png": true,
		".jpg": true}
	ext := path.Ext(file.Filename)
	if !File[ext] {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "文件格式错误",
			"data": nil,
		})
		return
	}
	dst := "D:\\gowork\\takeout-ordering\\pkg\\img\\" + file.Filename
	c.SaveUploadedFile(file, dst)

	go pkg.Upload(dst, file.Filename)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "图片上传成功",
		"data": dst,
	})

}

func CreateOrder(c *gin.Context) {
	var data model.CreateOrder
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	id := c.GetUint("userId")

	user, err := client.CreateOrder(c, &takeout.CreateOrderReq{
		MealsId:  data.MealsId,
		UserId:   int64(id),
		Num:      int64(data.Num),
		CouponId: data.CouponId,
		Notes:    data.Notes,
		Pay:      data.Pay,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "创建订单成功",
		"data": user,
	})
}

func OrderPay(c *gin.Context) {
	var data model.OrderPay
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	user, err := client.OrderPay(c, &takeout.OrderPayReq{
		OrderId: data.OrderId,
		Pay:     data.Pay,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "订单支付成功",
		"data": user,
	})
}

func Payback(c *gin.Context) {
	//OrderSn := c.Request.FormValue("out_trade_no")
	//Status := c.Request.FormValue("trade_status")
	//Total := c.Request.FormValue("total_amount")
	//
	//if Status == "TRADE_SUCCESS" {
	//	Status = "1"
	//}
	//status, err := strconv.Atoi(Status)
	//if err != nil {
	//	c.JSON(http.StatusOK, gin.H{
	//		"code": 0,
	//		"msg":  "转换失败",
	//		"data": nil})
	//	return
	//}
	//total, err := strconv.Atoi(Total)
	//if err != nil {
	//	c.JSON(http.StatusOK, gin.H{
	//		"code": 0,
	//		"msg":  "转换失败",
	//		"data": nil})
	//	return
	//}
	//payback, err := client.Payback(c, &takeout.PaybackReq{
	//	OrderSn: OrderSn,
	//	Status:  int64(status),
	//	Points:  int64(total),
	//})
	//if err != nil {
	//	c.JSON(http.StatusOK, gin.H{
	//		"code": 0,
	//		"msg":  err.Error(),
	//		"data": nil})
	//	return
	//}
	//
	//c.JSON(http.StatusOK, gin.H{
	//	"code": 200,
	//	"msg":  "支付宝回调成功",
	//	"data": payback,
	//})
}

func FindCoupon(c *gin.Context) {

	id := c.GetUint("userId")

	user, err := client.FindCoupon(c, &takeout.FindCouponReq{UserId: int64(id)})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "优惠卷列表展示成功",
		"data": user,
	})
}

func UserOrderList(c *gin.Context) {
	id := c.GetUint("userId")

	user, err := client.UserOrderList(c, &takeout.UserOrderListReq{UserId: int64(id)})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "订单列表展示成功",
		"data": user,
	})
}

func CouPonReceive(c *gin.Context) {
	var data model.CouPonReceive
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	id := c.GetUint("userId")
	user, err := client.CouPonReceive(c, &takeout.CouPonReceiveReq{
		UserId:   int64(id),
		CouPonId: data.CouPonId,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "领取优惠卷成功",
		"data": user,
	})
}
